<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>角色列表</title>
    <link rel="stylesheet" href="/static/plugins/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="/static/plugins/bootstrap-table/bootstrap-table.css">
    <link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
    <style>
        .box-padding-15 {
            padding: 15px;
        }
    </style>
</head>
<body>
<div class="layui-box box-padding-15">
    <div class="box-padding-15">
<span class="layui-breadcrumb">
  <a href="/" target="_top">首页</a>
  <a href="javascript:;">系统管理</a>
     <a><cite>角色管理</cite></a>
  <a><cite>列表</cite></a>
</span>
    </div>
<%--<div id="toolbar" class="btn-group">
    <button id="btn_add" type="button" class="btn btn-default">
        <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
    </button>
    <button id="btn_edit" type="button" class="btn btn-default">
        <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
    </button>
    <button id="btn_delete" type="button" class="btn btn-default">
        <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
    </button>
</div>--%>
<div id="toolbar" class="btn-group">
    <%--<c:forEach items="${btnList}" var="btn">
        <button id="${btn.btnId}" type="button" class="btn btn-default">
            <span class="glyphicon " aria-hidden="true"></span>${btn.name}
        </button>
    </c:forEach>--%>
    <button id="btn_add" type="button" class="btn btn-default">
        <span class="glyphicon " aria-hidden="true"></span>添加
    </button>
    <button id="btn_edit" type="button" class="btn btn-default">
        <span class="glyphicon " aria-hidden="true"></span>编辑
    </button>
    <button id="btn_delete" type="button" class="btn btn-default">
        <span class="glyphicon " aria-hidden="true"></span>删除
    </button>
</div>

<table id="tb_page"></table>
</div>
<script src="/static/plugins/jquery.js"></script>
<script src="/static/plugins/bootstrap/js/bootstrap.js"></script>
<script src="/static/plugins/bootstrap-table/bootstrap-table.js"></script>
<script src="/static/plugins/bootstrap-table/bootstrap-table-zh-CN.js"></script>
<script src="/static/plugins/layui/layui.js"></script>
<script>
    //JavaScript代码区域
    layui.use(['element','layer'], function(){
        var element = layui.element;
        var layer = layui.layer;
        $('#tb_page').bootstrapTable({
            url: '/role/pageData',         //请求后台的URL（*）
            method: 'get',                      //请求方式（*）
            toolbar: '#toolbar',                //工具按钮用哪个容器
            striped: true,                      //是否显示行间隔色
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            sortable: false,                     //是否启用排序
            sortOrder: "asc",                   //排序方式
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber:1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 25, 30, 50],        //可供选择的每页的行数（*）
            showRefresh: false,                  //是否显示刷新按钮
            clickToSelect: true,                //是否启用点击选中行
            height: 300,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            uniqueId: "id",                     //每一行的唯一标识，一般为主键列
            detailView: false,                   //是否显示父子表
            columns: [{
                checkbox: true
            }, {
                field: 'roleId',
                title: '编号',
                width:"25%"
            }, {
                field: 'roleName',
                title: '角色名称',
                width:"25%"
            },{
                field: 'isabled',
                title: '状态',
                width:"25%",
                formatter:function (value, row, index) {
                    var valStr = '';
                    switch (value){
                        case 0: valStr = "禁用";break;
                        case 1: valStr = "启用";break;
                    }
            return valStr;
        }
            },{
                field: 'decrib',
                title: '角色描述',
                width:"25%",

            }],
        });
        var $btnAdd = $('#btn_add');
        var $btnEdit = $('#btn_edit');
        var $btnDelete = $('#btn_delete');
        $btnAdd.on('click',add);
        $btnDelete.on('click',deleted);
        $btnEdit.on("click",edit);
    });

    function add() {
        location.href="/role/add";
    }

    function deleted() {
        var ids = [];
        var option =$("table[id=tb_page]").bootstrapTable("getSelections");
        if (option.length>0){
            $.each(option,function (i,item) {
                ids.push(item.roleId);
            })
        }
        if (ids.length<1){
            layer.msg("请勾选至少一条数据");
        }else {
            layer.confirm("确认删除所选数据？",function (index) {
                $.ajax({
                    url:"/role/delete",
                    type:"get",
                    dataType:"json",
                    data:{ids:ids.join(",")},
                    success:function (data) {
                        layer.msg(data.message);
                        if(data.code==1){
                            $('#tb_page').bootstrapTable("refresh");
                        }
                    },
                    error:function () {
                        layer.msg("服务器繁忙，请稍后再试！")
                    }
                })
            })
        }

    }
    function edit() {
        var ids = [];
        var option =$("table[id=tb_page]").bootstrapTable("getSelections");
        if (option.length>0){
            $.each(option,function (i,item) {
                ids.push(item.roleId);
            })
        }
        if(ids.length<1){
            layer.msg("请勾选一条数据");
        }else if(ids.length >1){
            layer.msg("只能勾选一条数据");
        }else {
            location.href = "/role/edit?roleId="+ids[0];
        }
    }
</script>
</body>
</html>
